// components/cascader/fulladdress.js
// var city = require("./area.js");
var sysdata = require('../../model/sysdata');
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: ''
    },
    customstyle: {
      type: String,
      value: ""
    },
    constyle: {
      type: String,
      value: ""
    },
    listtyle: {
      type: String,
      value: ""
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    citys: null,
    value: [],
    province_view: ""
  },
  ready: function () {


  },
  /**
   * 组件的方法列表
   */
  methods: {
    init(arr) {
      var that = this
      this.setData({
        show: true
      })
      if (!that.data.citys) {
        sysdata.citys().then(function (result) {
          if (result["status"] == 1) {
            console.log("ddddddddddddd")
            console.log(result)

            that.setData({
              citys: result["obj"]
            })
            // that.setselect([province,city,county])
            that.setselect(arr)
          }
        })
      } else {
        that.setselect(arr)
        // that.setselect([province,city,county])
      }


    },
    setselect(nams) {

      if (!nams) {

        return false
      }
      if (typeof (nams) == "string") {
        var reg = /.+?(省|市|自治区|自治州|县|区)/g;
        nams = nams.match(reg)
      }
      var that = this;
      var options = that.data.citys;
      var seletval = [];
      var selectnum = 0;
      for (var i = 0; i < 3; i++) {
        if (nams[i]) {
          var bvalue = false
          for (var a in options) {

            if (options[a].name.indexOf(nams[i]) > -1) {
              seletval.push({
                id: a,
                name: options[a].name
              })
              bvalue = true
              if (i != 2) {
                options = options[a].sub;
              }
              selectnum++
              break;

            }
          }
          if (!bvalue) {
            break;
          }

        }
      }

      if (seletval.length == 2) {
        seletval.push({
          id: seletval[1]['id'],
          name: "全市"
        })
      } else if (seletval.length == 1) {
        seletval.push({
          id: seletval[0]['id'],
          name: "全省"
        })
        seletval.push({
          id: seletval[0]['id'],
          name: "全省"
        })
      } else {
        this.onready()
      }
      // for (var n = selectnum; n < 3; n++) {
      //   seletval.push({ id: "", name: "请选择" })
      // }

      if (seletval.length > 0) {
        var update = {
          value: seletval
        };
        if (seletval[0]) {
          update['province_view'] = 'province_' + seletval[0]['id']
        }
        if (seletval[1]) {
          update['city_view'] = 'city_' + seletval[1]['id']
        }
        if (seletval[2]) {
          update['county_view'] = 'county_' + seletval[2]['id']
        }
        this.setData(update)
      }

      // if (selectnum == 3) {

      //   return { status: 1, seletval }
      // }
      // else {
      //   return { status: -1, seletval }
      // }
    },
    onready() {
      var value = this.data.value
      if (!value[0]) {
        value[0] = {
          id: 110000,
          name: '北京市'
        }
      }
      // if (!value[1]) {
      //   value[1] = JSON.parse(JSON.stringify(value[0]));
      //   value[1]['name'] = "全省"
      // }

      var update = {
        value: value
      };
      if (value[0]) {
        update['province_view'] = 'province_' + value[0]['id']
      }
      if (value[1]) {
        update['city_view'] = 'city_' + value[1]['id']
      }
      if (value[2]) {
        update['county_view'] = 'county_' + value[2]['id']
      }
      this.setData(update)

    },
    choose(e) {

      var dataset = e.currentTarget.dataset;
      var value = this.data.value;

      if (dataset.index == 0) {
        value[dataset.index] = {
          id: dataset.id,
          name: dataset.name
        }
        value[dataset.index * 1 + 1] = {
          id: dataset.id,
          name: "全省"
        }
      } else if (dataset.index == 1) {
        value[dataset.index] = {
          id: dataset.id,
          name: dataset.name
        }
      } else if (dataset.index == 2) {
        value[dataset.index] = {
          id: dataset.id,
          name: dataset.name
        }

      }


      this.setData({
        value: value
      })

      if (dataset.index == 2) {
        this.triggerEvent('chooseaddress', value, {})
        this.setData({
          show: false,
          value: []
        })
      } else if (dataset.index == 1) {
        if (value[1]['name'] == '全省') {
          value[2] = {
            id: value[1]['id'],
            name: value[1]['name']
          }
          this.triggerEvent('chooseaddress', value, {})
          this.setData({
            show: false,
            value: []
          })
        }

      }

    },
    close() {
      this.setData({
        show: false,
        value: []
      })
      this.triggerEvent('chooseaddress', null, {})
    },
    onclick() {

    }
  }
})